package com.batchap.classes;

import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ImageUtils {

	public static Image MixFrameAndImage(MySprite frame, Image photo, int photoPosX, int photoPosY)
	{
		Image imageResult = Image.createImage(240, 400);
		Graphics g = imageResult.getGraphics();
		if (photo != null)
			g.drawImage(photo, photoPosX, photoPosY, Graphics.LEFT | Graphics.TOP);
		frame.paint(g);// (frame, 0, 0, Graphics.LEFT | Graphics.TOP);		
				
		return imageResult;
	}
	
	//------------------------
	private static String photos = "fileconn.dir.photos";
	public static String path = System.getProperty(photos);
    
	public static void saveImage2File(Image photo, String name) {
		saveImage2File(PNGEncoder.toPNG(photo, false), name);
	}
	
	public static void saveImage2File(byte[] photo, String name) {
		// Receive a photo as byte array
		// Save Image to file
		FileConnection fileConn = null;
		DataOutputStream dos = null;

		try {
			fileConn = (FileConnection) Connector
					.open(path + name, Connector.READ_WRITE);
			if (!fileConn.exists()) {
				fileConn.create();
			}
			dos = new DataOutputStream(fileConn.openOutputStream());
			dos.write(photo);
			dos.flush();
			dos.close();
			fileConn.close();

		} catch (IOException ioe) {
			System.out.println("Error!" + ioe);
		}
	}
}
